<template>
	<el-main>
		<el-row :gutter="15">
			<el-col :lg="12">
				<el-card shadow="never" header="人员权限">
					<el-form :model="userForm" :rules="rules">
						<el-form-item label="系统管理员">
							<select-user-dialog v-model:accountId="userForm.adminAccount" :mode="false"/>
						</el-form-item>
						<el-form-item label="安全保密员">
							<select-user-dialog v-model:accountId="userForm.securityOfficer" :mode="false"/>
						</el-form-item>
						<el-form-item label="安全审计员">
							<select-user-dialog v-model:accountId="userForm.safetyAuditor" :mode="false"/>
						</el-form-item>
					</el-form>
					<div style="text-align: right;">
						<el-button type="primary" @click="saveUser">保存</el-button>
					</div>
				</el-card>
			</el-col>
			<el-col :lg="12">
				<el-card shadow="never" header="模块权限">
					<el-container>
						<el-header>
							<div class="left-panel">
								<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
							</div>
						</el-header>
						<el-main class="nopadding">
							<scTable ref="table" :apiObj="apiObj" row-key="configId"  stripe>
								<el-table-column label="#" type="index" width="50"></el-table-column>
								<el-table-column label="模块名称" prop="title" width="80"></el-table-column>
								<el-table-column label="创建权限" prop="insertRole" width="80">
									<template #default="scope">
										<el-switch v-model="scope.row.insertRole" active-value="1" inactive-value="0" :loading="scope.row.$switch_insertRole" @change="changeSwitchInsertRole($event, scope.row)" ></el-switch>
									</template>
								</el-table-column>
								<el-table-column label="删除权限" prop="deleteRole" width="80">
									<template #default="scope">
										<el-switch v-model="scope.row.deleteRole" active-value="1" inactive-value="0" :loading="scope.row.$switch_deleteRole" @change="changeSwitchDeleteRole($event, scope.row)" ></el-switch>
									</template>
								</el-table-column>
								<el-table-column label="更新权限" prop="updateRole" width="80">
									<template #default="scope">
										<el-switch v-model="scope.row.updateRole" active-value="1" inactive-value="0" :loading="scope.row.$switch_updateRole" @change="changeSwitchUpdateRole($event, scope.row)"></el-switch>
									</template>
								</el-table-column>
								<el-table-column label="更新权限" prop="queryRole" width="80">
									<template #default="scope">
										<el-switch v-model="scope.row.queryRole" active-value="1" inactive-value="0" :loading="scope.row.$switch_queryRole" @change="changeSwitchQueryRole($event, scope.row)" ></el-switch>
									</template>
								</el-table-column>
								<el-table-column label="是否启用" prop="status" width="80">
									<template #default="scope">
										<el-switch v-model="scope.row.status" active-value="1" inactive-value="0"  :loading="scope.row.$switch_status" @change="changeSwitch($event, scope.row)"></el-switch>
									</template>
								</el-table-column>
								<el-table-column label="备注" prop="remark" width="200"></el-table-column>
								<el-table-column label="操作" fixed="right" align="right" width="120">
									<template #default="scope">
										<el-button-group>
											<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
											<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
												<template #reference>
													<el-button text type="primary" size="small">删除</el-button>
												</template>
											</el-popconfirm>
										</el-button-group>
									</template>
								</el-table-column>
							</scTable>
						</el-main>
					</el-container>
				</el-card>
			</el-col>
		</el-row>
	</el-main>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import SelectUserDialog from "@/views/module/selectUserDialog";
import saveDialog from './save'
export default {
	name: 'System Security Setting',
	components: {
		SelectUserDialog,
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			apiObj: this.$API.system.sysSecurity.getSysSecurityConfigList,
			userForm:{
				adminAccount:'',
				securityOfficer:'',
				safetyAuditor:''
			},
			moduleForm:{

			},
			rules:[]
		}
	},
	mounted() {
		this.getSysSecurityUser();
	},
	methods: {
		async getSysSecurityUser() {
			var res = await this.$API.system.sysSecurity.getSysSecurityUser.get();
			if (res.code == 200) {
				this.userForm.adminAccount = res.data.adminAccount;
				this.userForm.securityOfficer = res.data.securityOfficer;
				this.userForm.safetyAuditor = res.data.safetyAuditor;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async saveUser(){
			var res = await this.$API.system.sysSecurity.setSysSecurityUser.post(this.userForm);
			if (res.code == 200) {
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//增加
		add(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		table_edit(row,index)
		{
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		handleSuccess(){
			this.$refs.table.refresh();
		},
		async table_del(row, index){
			var res = await this.$API.system.sysSecurity.deleteSysSecurityConfig.post({configId: row.configId});
			if(res.code == 200){
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			}else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async changeSwitchInsertRole(val, row) {
			row.insertRole = row.insertRole == '1' ? '0' : '1'
			row.$switch_insertRole = true;
			delete row.$switch_insertRole;
			row.insertRole = val;
			let res = await this.$API.system.sysSecurity.updateSysSecurityConfig.post({configId: row.configId, insertRole: row.insertRole});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async changeSwitchDeleteRole(val, row) {
			row.deleteRole = row.deleteRole == '1' ? '0' : '1'
			row.$switch_deleteRole = true;
			delete row.$switch_deleteRole;
			row.deleteRole = val;
			let res = await this.$API.system.sysSecurity.updateSysSecurityConfig.post({configId: row.configId, deleteRole: row.deleteRole});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async changeSwitchUpdateRole(val, row) {
			row.updateRole = row.updateRole == '1' ? '0' : '1'
			row.$switch_updateRole = true;
			delete row.$switch_updateRole;
			row.updateRole = val;
			let res = await this.$API.system.sysSecurity.updateSysSecurityConfig.post({configId: row.configId, updateRole: row.updateRole});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//表格内开关
		async changeSwitchQueryRole(val, row) {
			row.queryRole = row.queryRole == '1' ? '0' : '1'
			row.$switch_queryRole = true;
			delete row.$switch_queryRole;
			row.queryRole = val;
			let res = await this.$API.system.sysSecurity.updateSysSecurityConfig.post({configId: row.configId, queryRole: row.queryRole});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async changeSwitch(val, row) {
			row.status = row.status == '1' ? '0' : '1'
			row.$switch_status = true;
			delete row.$switch_status;
			row.status = val;
			let res = await this.$API.system.sysSecurity.updateSysSecurityConfig.post({configId: row.configId, status: row.status});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
	}
}
</script>

<style scoped>
</style>
